/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Logic;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/**
 *
 * @author Fernando
 */
public class Regla implements Serializable{
    private String servidor;
    private String tablespace;
    private String tabla;
    private String colummna;
    private String condicion;
    private String predicado;
    private String nombre;
    
    public Regla(){
        this.servidor = "";
        this.tablespace = "";
        this.tabla = "";
        this.colummna = "";
        this.condicion = "";
        this.predicado = "";
        this.nombre = "";
    }
    
    public Regla(String ser, String tsp, String tbl, String col, String con,String pred,int unN){
        this.servidor = ser;
        this.tablespace = tsp;
        this.tabla = tbl;
        this.colummna = col;
        this.condicion = con;
        this.predicado = pred;
        String enteroString = Integer.toString(unN);
        if(unN < 10){
            enteroString ="0"+enteroString;
        }
        this.nombre = this.servidor+"_R"+enteroString;
    }

    
    public String getServidor() {
        return servidor;
    }

    
    public void setServidor(String servidor) {
        this.servidor = servidor;
    }

    
    public String getTablespace() {
        return tablespace;
    }

    
    public void setTablespace(String tablespace) {
        this.tablespace = tablespace;
    }

    
    public String getTabla() {
        return tabla;
    }

    
    public void setTabla(String tabla) {
        this.tabla = tabla;
    }

    
    public String getColummna() {
        return colummna;
    }

    
    public void setColummna(String colummna) {
        this.colummna = colummna;
    }

    
    public String getCondicion() {
        return condicion;
    }

    
    public void setCondicion(String condicion) {
        this.condicion = condicion;
    }
    
    public String getNombre() {
        return nombre;
    }
    public String getPredicado() {
        return predicado;
    }

    
    public void setPredicado(String predicado) {
        this.predicado = predicado;
    }

    
    
    
    @Override
    public String toString(){
        StringBuilder s = new StringBuilder();
        s.append(String.format(this.nombre + "\n"));
        s.append(String.format(this.servidor + "\n"));    
        s.append(String.format(this.tablespace + "\n"));
        s.append(String.format(this.tabla + "\n"));
        s.append(String.format(this.colummna + "\n"));
        s.append(String.format(this.condicion + "\n"));
        s.append(String.format(this.predicado + "\n"));     
        return s.toString();
    }
    
    public String toLog(){
        StringBuilder s = new StringBuilder();
        s.append(String.format("Name:"+this.nombre + "/"));
        s.append(String.format("Server:"+this.servidor + "/"));    
        s.append(String.format("Tablespace:"+this.tablespace + "/"));
        s.append(String.format("Table:"+this.tabla + "/"));
        s.append(String.format("Column:"+this.colummna + "/"));
        s.append(String.format("Condition:"+this.condicion + "/"));
        s.append(String.format("Predicate:"+this.predicado + "/"));     
        return s.toString();
    }
    
    public boolean guardar(String arch) {
        try {
            FileOutputStream fileOut = new FileOutputStream(arch, true);
            ObjectOutputStream out = new ObjectOutputStream(fileOut);
            out.writeObject(this);
            out.close();
            fileOut.close();
        } catch (IOException i) {
            i.printStackTrace();
            return false;
        }

        return true;
    }
    
    public String antonimo() {
        String ant = "";
        switch (this.condicion) {
            case "=": {
                ant = "!=";
                break;
            }
            case "!=": {
                ant = "=";
                break;
            }
            case "<=": {
                ant = ">";
                break;
            }
            case "<": {
                ant = ">=";
                break;
            }
            case ">=": {
                ant = "<";
                break;
            }
            case ">": {
                ant = "<=";
                break;
            }
            default: {
                ant = null;
            }
        }
        return ant;
    }   
    
}
